using System;
using System.Configuration;
using System.Globalization;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Security.Cryptography;
using System.Web.UI.WebControls;

namespace Autenticazione
{
	public class Tools
	{
		public static int GetMaxItemsForPage()
		{
			int nMax = 10;
			object nMaxFromConfig = ConfigurationManager.AppSettings["MaxItemsForPage"];
			if (nMaxFromConfig != null)
			{
				nMax = int.Parse(nMaxFromConfig.ToString());
			}
			return nMax;
		}
		public static string CryptText(string txtToCrypt)
		{
			byte[] key = Convert.FromBase64String("a5GnfX3bZKG=");
			byte[] iv = Convert.FromBase64String("c2NhdG9sYQA=");
			if (!string.IsNullOrEmpty(txtToCrypt))
			{
				DESCryptoServiceProvider dcsp = new DESCryptoServiceProvider();
				MemoryStream ms = new MemoryStream();
				CryptoStream cs = new CryptoStream(ms, dcsp.CreateEncryptor(key, iv), CryptoStreamMode.Write);
				StreamWriter sw = new StreamWriter(cs);
				sw.Write(txtToCrypt);
				sw.Flush();
				cs.FlushFinalBlock();
				ms.Flush();
				return Convert.ToBase64String(ms.GetBuffer(), 0, (int)ms.Length);
			}
			return "";
		}
		public static void FillCombo(DropDownList ctrl, object container, string value, string text)
		{
			ctrl.Items.Clear();
			ctrl.DataTextField = text;
			ctrl.DataValueField = value;
			ctrl.DataSource = container;
			ctrl.DataBind();
		}
		public static void FillComboMesi(DropDownList ctrl)
		{
			ctrl.Items.Clear();
			for (int i = 1; i <= 12; i++)
			{
				ListItem item = new ListItem();
				item.Value = i.ToString();
				item.Text = DateTimeFormatInfo.CurrentInfo.GetMonthName(i);
				ctrl.Items.Add(item);
			}
		}
		public static void FillComboDaysInMonthSelected(DropDownList ctrl, int month, int year)
		{
			ctrl.Items.Clear();
			int days = DateTime.DaysInMonth(year, month);
			for (int i = 1; i <= days; i++)
			{
				string sDay = string.Format("0{0}", i.ToString());
				ListItem item = new ListItem(sDay.Substring(sDay.Length - 2), sDay.Substring(sDay.Length - 2));
				ctrl.Items.Add(item);
			}
		}
		public static void FillComboYears(DropDownList ctrl)
		{
			int minYear = int.Parse(ConfigurationManager.AppSettings["MinYear"]);
			int maxYear = DateTime.Today.Year;
			for (int i = minYear; i <= maxYear; i++)
			{
				ListItem item = new ListItem(i.ToString(), i.ToString());
				ctrl.Items.Add(item);
			}
		}
		public static void FillComboGender(DropDownList ctrl)
		{
			ctrl.Items.Clear();
			ListItem item = new ListItem("Maschile", "0");
			ctrl.Items.Add(item);
			item = new ListItem("Feminile", "1");
			ctrl.Items.Add(item);
		}
		public static void FillGrapplingLevel(DropDownList ctrl)
		{
			ctrl.Items.Clear();
			ListItem item = new ListItem(" ", " ");
			ctrl.Items.Add(item);
			item = new ListItem("A", "A");
			ctrl.Items.Add(item);
			item = new ListItem("B", "B");
			ctrl.Items.Add(item);
		}
		public static void MandaMail(string destinationAddr, string sourceAddr, string subject, string content)
		{
			SmtpClient mail = new SmtpClient();
			mail.Host = "smtp.aruba.it";
			mail.EnableSsl = true;
			mail.Credentials = new NetworkCredential
			{
				UserName = "postmaster@wapsacitalia.it", 
				Password = "marco123"
			};
			mail.Port = 465;
			MailAddress source = new MailAddress(sourceAddr);
			MailAddress destination = new MailAddress(destinationAddr);
			MailMessage message = new MailMessage(source, destination);
			message.Body = content;
			message.Subject = subject;
			mail.Send(message);
			message.Dispose();
		}
	}
}
